import java.util.Scanner;

public class Demo21 {
    private static boolean flag = true;
    public static void main(String[] args) {
        Object locker = new Object();
        Thread t1 = new Thread(() -> {
            while (flag) {
//                try {
//                    Thread.sleep(1);
//                } catch (InterruptedException e) {
//                    throw new RuntimeException(e);
//                }
            }
            System.out.println("t1 结束");
        });

        Thread t2 = new Thread(() -> {
            // 让用户进行输入, 根据用户输入, 修改 flag
            System.out.println("请输入任意内容, 终止线程 t1");
            Scanner scanner = new Scanner(System.in);
            scanner.next();
            // 通过修改 flag 让 t1 线程结束.
            flag = false;
            System.out.println("flag = " + flag);
        });

        t1.start();
        t2.start();
    }
}
